package com.wenshi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wenshi.common.utils.DateUtils;
import com.wenshi.system.domain.Alarm;
import com.wenshi.system.domain.SysDevice;
import com.wenshi.system.mapper.AlarmMapper;
import com.wenshi.system.service.GetDeviceStatusService;
import com.wenshi.system.service.ISysAlarmService;
import com.wenshi.system.service.ISysDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysAlarmServiceImpl extends ServiceImpl<AlarmMapper,Alarm> implements ISysAlarmService {

    @Autowired
    private ISysDeviceService deviceService;
    @Autowired
    private GetDeviceStatusService getDeviceStatusService;
    @Override
    public List<Alarm> getByDeviceIpList(String deviceIp) {
        SysDevice byDeviceIp = deviceService.getByDeviceIp(deviceIp);
        QueryWrapper<Alarm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_id",byDeviceIp.getDeviceId());
        queryWrapper.eq("alarm_status",1).orderByDesc("clear_time").orderByDesc("alarm_time");
        List<Alarm> alarms = baseMapper.selectList(queryWrapper);
        return alarms;
    }

    @Override
    public List<Alarm> getNewAlarm(String deviceIp) {
        SysDevice byDeviceIp = deviceService.getByDeviceIp(deviceIp);
        QueryWrapper<Alarm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_id",byDeviceIp.getDeviceId());
        queryWrapper.eq("alarm_status",0).orderByDesc("alarm_time");
        List<Alarm> alarms = baseMapper.selectList(queryWrapper);
        return alarms;
    }

    @Override
    public boolean clear(Alarm alarm) {
        UpdateWrapper<Alarm> wrapper = new UpdateWrapper<>();
        wrapper.eq("alarm_id",alarm.getAlarmId());
        wrapper.set("alarm_status",true);
        int i = baseMapper.update(alarm,wrapper);
        if (i>0){

            return true;
        }
        return  false;
    }

    @Override
    public void resetAllAlarm(String deviceIp) {
        SysDevice sysDevice = deviceService.getByDeviceIp(deviceIp);
        int[][] address ={{2714,2714}};
        boolean test = getDeviceStatusService.onDevice(deviceIp, address);
        List<Alarm> alarms = new ArrayList<>();
        if (test){
            QueryWrapper<Alarm> wrapper = new QueryWrapper<Alarm>().eq("device_id", sysDevice.getDeviceId());
            List<Alarm> alarmList = baseMapper.selectList(wrapper);
            for (Alarm alarm : alarmList) {
                alarm.setAlarmStatus(true);
                alarm.setClearTime(DateUtils.getTime());
                alarms.add(alarm);
            }
        }
        this.saveOrUpdateBatch(alarms);
    }

    @Override
    public boolean clearAllAlarm(String deviceIp) {
        List<Alarm> clearAlarms = getByDeviceIpList(deviceIp);
        return  this.removeBatchByIds(clearAlarms);
    }
}
